package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.widget.Toast;

import com.google.gson.Gson;
import com.youzan.spiderman.utils.MD5Utils;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.request.AccessTokenRequest;
import sz.szsmk.citizencard.model.network.bean.request.GetOrderOpenid;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;
import sz.szsmk.citizencard.model.network.bean.request.QueryCardFuncRequest;
import sz.szsmk.citizencard.model.network.bean.request.UserBehaviorBrowseReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.FirstSearchUserLevelRes;
import sz.szsmk.citizencard.model.network.bean.response.GetOrderOpenidRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryAllCardFuncRes;
import sz.szsmk.citizencard.model.network.bean.response.SeleciUserAuthRes;
import sz.szsmk.citizencard.model.network.bean.response.ValidCardAuthorizationRes;
import sz.szsmk.citizencard.mvp.ElecCitizenCardMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.ui.adapter.bean.AppHomeCardBean;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.StringUtils;

public class ElecCitizenCardPresenter extends AppBasePresenter<ElecCitizenCardMvpView> {
    NetworkDateSources mNetworkDateSource;

    @Inject
    public ElecCitizenCardPresenter() {
        getComponent().inject(this);
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    public void firstSearchUserLevel() {
        IBaseRequest baseRequest = new IBaseRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        baseRequest.setAccess_token(access_token);
        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());
        baseRequest.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        String refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token");
        String key = "access_token=" + access_token + "&device_id=" + androidId + "&key=" + refresh_token;
        String stringMd5 = MD5Utils.getStringMd5(key).toUpperCase();
        baseRequest.setSign(stringMd5);
        mNetworkDateSource.firstSearchUserLevel(baseRequest, new SubscribeResult<FirstSearchUserLevelRes>() {
            @Override
            protected void onOk(FirstSearchUserLevelRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "json---" + json);
                if (response.getData() == null || response.getData().getRecognitionLevel() == null) {
                    getMvpView().firstSearchUserLevelFail();
                    return;
                }
                AppHomeCardBean appHomeCardBean = new AppHomeCardBean();
                if (!StringUtils.isBlank(response.getData().getRecognitionLevel().getL3())) {
                    appHomeCardBean.setLevel("L3");
                    appHomeCardBean.setLevelDesc(response.getData().getRecognitionLevel().getL3());
                } else if (!StringUtils.isBlank(response.getData().getRecognitionLevel().getL2())) {
                    appHomeCardBean.setLevel("L2");
                    appHomeCardBean.setLevelDesc(response.getData().getRecognitionLevel().getL2());
                } else if (!StringUtils.isBlank(response.getData().getRecognitionLevel().getL1())) {
                    appHomeCardBean.setLevel("L1");
                    appHomeCardBean.setLevelDesc(response.getData().getRecognitionLevel().getL1());
                }
                if (!StringUtils.isBlank(response.getData().getRecognitionLevel().getL2())) {
                    String[] split = response.getData().getRecognitionLevel().getL2().split("\\+");
                    appHomeCardBean.setIdCard(split[1]);
                }
                appHomeCardBean.setCustName(response.getData().getCustName());
                appHomeCardBean.setEcityCardNo(response.getData().getEcityCardNo());
                getMvpView().firstSearchUserLevelSucc(appHomeCardBean);
            }

            @Override
            protected void onFailure(FirstSearchUserLevelRes response) {
                super.onFailure(response);
                getMvpView().firstSearchUserLevelFail();
            }

            @Override
            protected void onNext(FirstSearchUserLevelRes response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().firstSearchUserLevelFail();
            }
        });
    }





    public void validCardAuthorization() {
        IBaseRequest baseRequest = new IBaseRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        baseRequest.setAccess_token(access_token);
        mNetworkDateSource.validCardAuthorization(baseRequest, new SubscribeResult<ValidCardAuthorizationRes>() {
            @Override
            protected void onOk(ValidCardAuthorizationRes response) {

                //autype:1.休闲年卡 2.体育惠民3.文化消费
                //isAuth:0.不存在1.已授权2.未授权且有多张休闲年卡
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "json---" + json);
                getMvpView().validCardAuthorizationSucc(response);

            }

            @Override
            protected void onFailure(ValidCardAuthorizationRes response) {
                super.onFailure(response);
                getMvpView().validCardAuthorizationFail(response);
            }

            @Override
            protected void onNext(ValidCardAuthorizationRes response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    public void queryCardFunc() {

        QueryCardFuncRequest queryCardFuncRequest = new QueryCardFuncRequest();
        String json = new Gson().toJson(queryCardFuncRequest);
        L.e(Constant.TAG, "查询卡功能入参---" + json);
        mNetworkDateSource.quryAllCardsFunction(queryCardFuncRequest, new SubscribeResult<QueryAllCardFuncRes>() {
            @Override
            protected void onOk(QueryAllCardFuncRes response) {
                getMvpView().queryCardFuncSucc(response);
            }

            @Override
            protected void onFailure(QueryAllCardFuncRes response) {
                super.onFailure(response);
                getMvpView().queryCardFuncFail(response);

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void selectUserAuth() {
        AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
        mNetworkDateSource.selectUserAuth(accessTokenRequest, new SubscribeResult<SeleciUserAuthRes>() {
            @Override
            protected void onOk(SeleciUserAuthRes response) {
                String json = new Gson().toJson(response);
                getMvpView().selectUserAuthSucc(response);
            }

            @Override
            protected void onFailure(SeleciUserAuthRes response) {
                super.onFailure(response);
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void getExtent(final String url) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");

        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setTag("HUIMIN");
        getOrderOpenid.setAccess_token(access_token);
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                getMvpView().getExtentSucc(response,url);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {

                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    public void getGetOrderId(final String type, final String url) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("HUIMIN");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                Constant.EXTENT = response.getExtend();
                getMvpView().getGetOrderIdSucc(response,type,url);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {
                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    public void userBehaviorBrowse() {
        UserBehaviorBrowseReq userBehaviorBrowseReq = new UserBehaviorBrowseReq();
        userBehaviorBrowseReq.setModularId( 47  );
        userBehaviorBrowseReq.setAccessToken(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        mNetworkDateSource.userBehaviorBrowse(userBehaviorBrowseReq, new Action1<BaseBean>() {
            @Override
            public void call(BaseBean baseBean) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }


}
